package tijava.container;

import java.util.PriorityQueue;
import java.util.Queue;

import tijava.container.type.ComparableType;
import tijava.container.type.EqualsComparableType;

/**
 * 使用PriorityQueue的类必须实现Comparable接口
 * 
 * @see tijava.container.type.ComparableType
 * @author nathan
 * 
 */
public class PriorityQueueDemo {
	public static void main(String[] args) {
		justTestComparableType();
		testEqualsComparableType();
	}

	/**
	 * 只实现Comparable接口的类型，不支持查找和删除等操作
	 */
	private static void justTestComparableType() {
		Queue<ComparableType> q = new PriorityQueue<ComparableType>();
		q.add(new ComparableType('C', 4, "Empty trash"));
		q.add(new ComparableType('A', 2, "Feed dog"));
		q.add(new ComparableType('B', 7, "Feed bird"));
		q.add(new ComparableType('C', 3, "Mow lawn"));
		q.add(new ComparableType('A', 1, "Water lawn"));
		q.add(new ComparableType('B', 1, "Feed cat"));

		System.out.println("the queue's size is:" + q.size());

		// not found
		if (q.contains(new ComparableType('C', 4, "Empty trash"))) {
			System.out.println("e found in set");
		} else {
			System.out.println("not found in set");
		}

		// remove failed!
		if (q.remove(new ComparableType('C', 4, "Empty trash"))) {
			System.out.println("e removed in set");
		} else {
			System.out.println("remove failed!");
		}

		// size is still 6
		System.out.println("the queue's size is:" + q.size());
	}

	/**
	 * 实现了equals方法后，查找、删除等操作工作就ok了
	 */
	private static void testEqualsComparableType() {
		Queue<EqualsComparableType> q = new PriorityQueue<EqualsComparableType>();
		q.add(new EqualsComparableType('C', 4, "Empty trash"));
		q.add(new EqualsComparableType('A', 2, "Feed dog"));
		q.add(new EqualsComparableType('B', 7, "Feed bird"));
		q.add(new EqualsComparableType('C', 3, "Mow lawn"));
		q.add(new EqualsComparableType('A', 1, "Water lawn"));
		q.add(new EqualsComparableType('B', 1, "Feed cat"));

		System.out.println("the queue's size is:" + q.size());

		// not found
		if (q.contains(new EqualsComparableType('C', 4, "Empty trash"))) {
			System.out.println("e found in set");
		} else {
			System.out.println("not found in set");
		}

		// remove failed!
		if (q.remove(new EqualsComparableType('C', 4, "Empty trash"))) {
			System.out.println("e removed in set");
		} else {
			System.out.println("remove failed!");
		}

		// size is still 6
		System.out.println("the queue's size is:" + q.size());
	}
}
